package com.example.administrator.funchat.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.funchat.Contans.Config;
import com.example.administrator.funchat.R;
import com.example.administrator.funchat.adapter.ChatAdapter;
import com.example.administrator.funchat.model.Chat_information;
import com.example.administrator.funchat.model.User;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/24.
 */
public class ChatActivity extends AppCompatActivity {

    private ListView chatListview;
    private EditText inputText;
    private ImageView send;
    private ImageView iv_back_chat;
    private ChatAdapter adapter;
    private List<Chat_information> chatList = new ArrayList<>();
    public static List<String> tempChat = new ArrayList<>();
    public static String msg_send;//发消息者

    public static final String KEY_MESSAGE = "message";


    public static final String MESSAGE_RECEIVED_ACTION = "com.example.funchat.MESSAGE_RECEIVED_ACTION";
    public static boolean isForeground = false;

    private User friendInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.chat_layout);
        iv_back_chat = (ImageView) findViewById(R.id.iv_back_chat);
        iv_back_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        friendInfo = (User) getIntent().getSerializableExtra("friendInfo");//聊天好友信息
        //加载聊天截面数据并设置监听事件
        initChats();
        adapter = new ChatAdapter(ChatActivity.this, R.layout.chat_list, chatList);
        inputText = (EditText) findViewById(R.id.input);
        send = (ImageView) findViewById(R.id.send);
        chatListview = (ListView) findViewById(R.id.chat_list);
        chatListview.setAdapter(adapter);


        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputText.getText().toString();
                if (!"".equals(content)) {

                    org.xutils.http.RequestParams params = new org.xutils.http.RequestParams(Config.SEND_MESSAGE);
                    params.addBodyParameter("mine_phone", MainActivity.u.getPhone());
                    params.addBodyParameter("friend_phone", friendInfo.getPhone());
                    params.addBodyParameter("content", content);

                    x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            if (s.equals("ok")) {
                                Toast.makeText(ChatActivity.this, "发送成功", Toast.LENGTH_SHORT).show();

                            }

                        }

                        @Override
                        public void onError(Throwable throwable, boolean b) {

                        }

                        @Override
                        public void onCancelled(CancelledException e) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });

                    Chat_information infoumation = new Chat_information(content, friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_SENT);
                    chatList.add(infoumation);
                    adapter.notifyDataSetChanged();
                    chatListview.setSelection(chatList.size());
                    inputText.setText("");
                }
            }
        });
        registerReceive();//注册广播

    }


    private void initChats() {
        Chat_information chat01 = new Chat_information("Helly,what are you doing now?", friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_RECEIVED);
        chatList.add(chat01);
        Chat_information chat02 = new Chat_information("sleeping,some thing wrong?", friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_SENT);
        chatList.add(chat02);
        Chat_information chat03 = new Chat_information("no,I just want to talk with you,how long no see,wo remeber that you like play basketball,Do you wanner play with us?there are peter sam and me", friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_RECEIVED);
        chatList.add(chat03);
        Chat_information chat04 = new Chat_information("Oh ,pretty! come to me now?", friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_SENT);
        chatList.add(chat04);
        Chat_information chat05 = new Chat_information("good!waiting for me ,gay", friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_RECEIVED);
        chatList.add(chat05);
        Chat_information chat06 = new Chat_information("come on", friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_SENT);
        chatList.add(chat06);
        if (msg_send != null && msg_send.equals(friendInfo.getPhone())) {
            if (tempChat.size() != 0) {
                Chat_information info;
                for (int i = 0; i < tempChat.size(); i++) {
                    info = new Chat_information(tempChat.get(i), friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_RECEIVED);
                    chatList.add(info);
                }
                tempChat.clear();
            }
        }
    }

    private myMessageReceive receiver;

    public void registerReceive() {
        receiver = new myMessageReceive();
        IntentFilter filter = new IntentFilter(MESSAGE_RECEIVED_ACTION);
        registerReceiver(receiver, filter);

    }

    class myMessageReceive extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String message = intent.getStringExtra(KEY_MESSAGE);
                Chat_information infoumation = new Chat_information(message, friendInfo.getHeadImg(), MainActivity.u.getHeadImg(), Chat_information.TYPE_RECEIVED);
                chatList.add(infoumation);
                adapter.notifyDataSetChanged();
                chatListview.setSelection(chatList.size());
            }

        }
    }

    @Override
    protected void onResume() {
        isForeground = true;
        Log.i("message", "onResume: " + isForeground);
        super.onResume();
    }

    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);//反注册
        super.onDestroy();
    }
}
